package com.glorysoft.hlgdpda.ui.materialandtool.view

import android.os.Bundle
import android.view.View
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityScrapBinding
import com.glorysoft.hlgdpda.entity.CodeListEntity
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.materialandtool.viewmodel.ScrapViewModel
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_scrap.defectEmptyView
import kotlinx.android.synthetic.main.activity_scrap.etCarrierUp
import kotlinx.android.synthetic.main.activity_scrap.etLot
import kotlinx.android.synthetic.main.activity_scrap.flDefectList
import kotlinx.android.synthetic.main.activity_scrap.ivDefectArrow
import kotlinx.android.synthetic.main.activity_scrap.rvDefectList
import kotlinx.android.synthetic.main.activity_scrap.rvUpMaterialList
import kotlinx.android.synthetic.main.activity_scrap.tvDefectTotal
import kotlinx.android.synthetic.main.activity_scrap.upMaterialEmptyView

/**
 * FileName: ScrapActivity
 * Author: XJH
 * Date: 2021/8/20 16:06
 * Description:
 * History:
 */
class ScrapActivity : BaseActivity<ScrapViewModel,ActivityScrapBinding>(), View.OnClickListener {

    var codePop : PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_scrap
    }

    override fun initVariableId(): Int {
        return BR.scrapViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("报废")
        setRightListener("提交"){
            viewModel.scrap()
        }

        rvUpMaterialList.setEmptyView(upMaterialEmptyView)
        rvDefectList.setEmptyView(defectEmptyView)
        flDefectList.setOnClickListener(this)
        viewModel.initDefectAdapter(rvDefectList,tvDefectTotal)
    }

    override fun initData() {
        initEdit(etLot,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchLotInfo(s)
            }

        })

        initEdit(etCarrierUp,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.searchCarrierInfo(s)
            }
        })

        viewModel.defUI.callObserve.observe(this){
            when(it.type){
                1 ->{
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确认删除", "我再想想", "是否确认删除该不良信息？",
                        {
                            viewModel.deleteInfo("defect",it.item)
                            val entity = it.item as CodeListEntity.CodeEntity
                            if (entity.MAINQTY != "" && entity.MAINQTY.toInt() > 0){
                                tvDefectTotal.text = (tvDefectTotal.text.toString().toInt() - entity.MAINQTY.toInt()).toString()
                            }
                        }) {}.show(supportFragmentManager,"DELETE_DIALOG")
                }
                200 -> {
                    val entity = it.item as LotEntity
                    etLot.setText(entity.LOT.LOTID)
                    etCarrierUp.setText("")
                }
            }
        }

        viewModel.defUI.startActivity.observe(this){
            etLot.setText("")
            etCarrierUp.setText("")
            tvDefectTotal.text = "0"
        }
    }

    override fun onClick(v: View?) {
        if (viewModel.defectCodeList.size <= 0){
            viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到不良代码信息",false)
            return
        }
        if (codePop == null){
            codePop = PopWindowsUtils.getOneChoosePop(this, flDefectList.width, viewModel.defectCodeList, ivDefectArrow,object :  PopWindowsUtils.PopTextAndPositionCallback{
                override fun onSuccess(string: String, potion: Int) {
                    viewModel.addItem(string)
                }
            }
            )
        }

        if (codePop!!.isShowing) {
            codePop?.dismiss()
        } else {
            PopWindowsUtils.setArrowShow(ivDefectArrow)
            codePop?.showAsDropDown(flDefectList, 0, 0)
        }
    }
}